package elitech.vietnam.weatherfashion.backgroundtask;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dto.MemberDTO;

public class ReloadDataService extends Service {

	public static boolean isRunning = false;
	private final int CYCLE_RELOAD = 120000; // 2 phut

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		isRunning = true;

		new Thread(new Runnable() {
			@Override
			public void run() {
				boolean isFirst = true;
				while (isRunning) {
					try {
						if (isFirst) {
							isFirst = false;
							Thread.sleep(30000);
							Utils.Log("ReloadDataService first");
						}

						Utils.Log("ReloadDataService getTodayData");
						Utils.getTodayData(getApplicationContext());
						Utils.Log("ReloadDataService getWeeklyData(0)");
						Utils.getWeeklyData(getApplicationContext(), 0);
						// AppUtils.Log("result getWeeklyData 0"
						// + AppConfig.getInstance().weeklyData.get(0));

						Utils.Log("ReloadDataService getWeeklyData(1)");
						Utils.getWeeklyData(getApplicationContext(), 1);
						// AppUtils.Log("result getWeeklyData 1"
						// + AppConfig.getInstance().weeklyData.get(1));

						Utils.Log("ReloadDataService getWeeklyData(2)");
						Utils.getWeeklyData(getApplicationContext(), 2);
						// AppUtils.Log("result getWeeklyData 2"
						// + AppConfig.getInstance().weeklyData.get(2));

						Utils.Log("ReloadDataService getWeeklyData(3)");
						Utils.getWeeklyData(getApplicationContext(), 3);
						// AppUtils.Log("result getWeeklyData 3"
						// + AppConfig.getInstance().weeklyData.get(3));

						Utils.Log("ReloadDataService getWeeklyData(4)");
						Utils.getWeeklyData(getApplicationContext(), 4);
						// AppUtils.Log("result getWeeklyData 4"
						// + AppConfig.getInstance().weeklyData.get(4));

						Utils.Log("ReloadDataService getStylerAllData()");
						Utils.getStylerAllData(getApplicationContext());
						
						MemberDTO user = GlobalValue.getInstance(
								getApplicationContext()).readUserInfo();
						if (user != null) {
							Utils.Log("ReloadDataService getFriendList()");
							Utils.getFriendList(getApplicationContext());
							
							Utils.Log("ReloadDataService getStylerFriendData()");
							Utils.getStylerFriendData(getApplicationContext());

							Utils.Log("ReloadDataService getSelfData()");
							Utils.getSelfData(getApplicationContext());
						}

						Utils.Log("ReloadDataService Sleeping, G9 all");
						Thread.sleep(CYCLE_RELOAD);
					} catch (Exception ex) {

					}
				}
				Utils.Log("Out ReloadDataService looping ");
			}
		}).start();

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		isRunning = true;
		return null;
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Utils.Log("onUnbind ReloadDataService");
		return super.onUnbind(intent);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		isRunning = false;
		Utils.Log("onDestroy ReloadDataService ");
	}

}
